home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / xinetd / pset.1.1.1 / pset.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  1.9 KB  |  108 lines

  1. /*
  2.  * (c) Copyright 1992 by Panagiotis Tsirigotis
  3.  * All rights reserved.  The file named COPYRIGHT specifies the terms 
  4.  * and conditions for redistribution.
  5.  */
  6.  
  7. static char RCSid[] = "$Id: pset.c,v 2.1 1992/10/10 07:31:48 panos Exp $" ;
  8. static char version[] = VERSION ;
  9.  
  10. #include "pset.h"
  11.  
  12. #define NULL                        0
  13. #define ALLOC_START                20
  14. #define ALLOC_STEP                10
  15.  
  16. #define POINTER                    __pset_pointer
  17.  
  18. char *malloc(), *realloc() ;
  19.  
  20.  
  21. pset_h pset_create( alloc_start, alloc_step )
  22.     unsigned alloc_start, alloc_step ;
  23. {
  24.     pset_h pset ;
  25.     unsigned start ;
  26.  
  27.     pset = (pset_h) malloc( sizeof( struct __pset ) ) ;
  28.     if ( pset == NULL )
  29.         return( NULL ) ;
  30.     
  31.     start = ( alloc_start == 0 ) ? ALLOC_START : alloc_start ;
  32.     pset->ptrs = (POINTER *) malloc( start * sizeof( POINTER ) ) ;
  33.     if ( pset->ptrs == NULL )
  34.     {
  35.         free( (char *) pset ) ;
  36.         return( NULL ) ;
  37.     }
  38.  
  39.     pset->max = start ;
  40.     pset->count = 0 ;
  41.     pset->alloc_step = ( alloc_step == 0 ) ? ALLOC_STEP : alloc_step ;
  42.     return( pset ) ;
  43. }
  44.  
  45.  
  46. void pset_destroy( pset )
  47.     pset_h pset ;
  48. {
  49.     free( (char *) pset->ptrs ) ;
  50.     free( (char *) pset ) ;
  51. }
  52.  
  53.  
  54. POINTER pset_insert( pset, p )
  55.     pset_h pset ;
  56.     POINTER p ;
  57. {
  58.     if ( pset->count >= pset->max )
  59.     {
  60.         unsigned new_max = pset->max + pset->alloc_step ;
  61.         POINTER *new_ptrs ;
  62.  
  63.         new_ptrs = (POINTER *) realloc(
  64.                                 (char *)pset->ptrs, new_max * sizeof( POINTER ) ) ;
  65.         if ( new_ptrs == NULL )
  66.             return( NULL ) ;
  67.         pset->max = new_max ;
  68.         pset->ptrs = new_ptrs ;
  69.     }
  70.     return( pset->ptrs[ pset->count++ ] = p ) ;
  71. }
  72.  
  73.  
  74. void pset_delete( pset, ptr )
  75.     register pset_h pset ;
  76.     register POINTER ptr ;
  77. {
  78.     register unsigned u = pset->count ;
  79.  
  80.     if ( u == 0 )
  81.         return ;
  82.     
  83.     do
  84.     {
  85.         u-- ;
  86.         if ( pset->ptrs[ u ] == ptr )
  87.         {
  88.             pset->ptrs[ u ] = pset->ptrs[ --pset->count ] ;
  89.             return ;
  90.         }
  91.     }
  92.     while ( u ) ;
  93. }
  94.  
  95.  
  96. psi_h psi_create( pset )
  97.     pset_h pset ;
  98. {
  99.     psi_h iter = (psi_h) malloc( sizeof( struct __pset_iterator ) ) ;
  100.  
  101.     if ( iter == NULL )
  102.         return( NULL ) ;
  103.     iter->pset = pset ;
  104.     return( iter ) ;
  105. }
  106.  
  107.  
  108.